<?php


class PageView {

    private $doctype ;
    private $styleSheets = array() ;
    private $charset ;

    protected $model ;

    /**
     *
     */
    public function __construct($model = null) {
        $this->setDoctype() ;
        $this->setCharset() ;

        // add model if set
        if($model !== null) {
            $this->model = $model ;
        }

    }
    /**
     * set the doctype
     *
     * @param string $doc If none set, set to XHTML 1.0
     */
    public function setDoctype($doc = "XHTML 1.0") {

        if ($doc == "XHTML 1.0") {
            $this->doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ;
        }
    
    }
    
    /**
     * set the charset
     *
     * @param string $type If none set, set to utf-8
     */
    public function setCharset($type = "utf-8") {
        $this->charset = $type ;
    }


    /**
     * add a stylesheet
     *
     * @param string $href Href to the stylesheet-file
     */
    public function addStyleSheet($href) {
        $this->styleSheets[] = "<link rel=\"StyleSheet\" href=\"$href\" type=\"text/css\" />" ; 
    }
    
    /**
     * Build the header from the class-variables
     *
     * @return string $retStr
     */ 
    public function getHeader($title = "") {
        $retStr = "" ;

        $retStr .= "<head>" ;
        $retStr .= "<meta http-equiv=\"content-type\" content=\"text/html; charset=$this->charset\">" ;

        $retStr .= "<title>$title</title>" ;

        foreach ($this->styleSheets as $style) {
            echo $style ;
            $retStr .= $style ;
        }

        $retStr .= "</head>" ;

        return $retStr ;
    
    }
    
    /**
     * set everything together
     *
     * @param array $attr Could be $attr['title'] or $attr['body'], is extendable
     *
     * @return string $XHtml
     */
    public function getXHtml(array $attr = array()) {
        $XHtml = "" ;
        $XHtml .= $this->doctype ;
        $XHtml .= "<html>" ;
        $XHtml .= $this->getHeader((isset($attr['title'])) ? $attr['title'] : Null) ;
        $XHtml .= "<body>" ;
        $XHtml .= ($attr['body']) ? $attr['body'] : "No information" ;
        $XHtml .= "</body>" ;
        $XHtml .= "</html>" ;
        return $XHtml ;
            
    }

    /**
     * Function to make a div
     *
     * @param string $content The content in the div
     * @param array $attr The attributes for the div
     *
     * @return str $retStr The created div in xhtml-format
     */
    public function addDiv($content, $attr = array()) {
        $retStr ="<div " ;

        foreach($attr as $key => $value) {
            $retStr .= " $key=\"$value\" " ;
        }

        $retStr .= ">$content</div>" ;
        
        return $retStr;
    }

    /**
     * Adds a notice, a styled p-tag
     *
     * @param string $content
     * @param string $noticeType The class the notice should have
     *
     * @return string
     */
    public function addNotice($content, $noticeType = "notice") {
        return "<p class=\"$noticeType\">$content</p>" ;
    }



}
